package com.bytedance.frameworks.core.a;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class e {
    private static e e;
    public final HashMap a;
    public final HashMap b;
    private final LinkedList f;
    private final AtomicBoolean g;
    d c;
    com.bytedance.frameworks.core.a.b d;
    private int h;
    private int i;
    private JSONObject j;
    private i k;

    private e(Context arg4) {

        this.a = new HashMap();
        this.b = new HashMap();
        this.f = new LinkedList();
        this.g = new AtomicBoolean(false);
        this.h = 120;
        this.i = 100;
        arg4.getApplicationContext();
        this.c = com.bytedance.frameworks.core.a.d.a(arg4);
        this.k = new f(arg4, "monitor");
        i v0 = this.k;
        if (v0 != null) {
            android.support.design.a.h = v0;
        }

        this.d = new com.bytedance.frameworks.core.a.b(this.f, this, this.g);
        this.d.start();
        this.b();
    }

    public static e a(Context arg2) {
        if (e == null) {
            Class v1 = e.class;
            synchronized (v1) {
                if (e == null) {
                    e = new e(arg2);
                }
            }
        }

        return e;
    }

    private static JSONObject a(String arg6, a arg7, b arg8) {
        JSONObject v1 = null;
        JSONObject v0 = new JSONObject();
        if (arg6.equals("count")) {
            try {
                v0.put("type", arg7.b);
                v0.put("key", arg7.a);
                v0.put("value", ((double) arg7.c));
            } catch (Exception v0_1) {
                v0 = v1;
            }

            return v0;
        }

        if (arg6.equals("timer")) {
            try {
                v0.put("type", arg8.b);
                v0.put("key", arg8.a);
                v0.put("value", ((double) (arg8.d / (((float) arg8.c)))));
            } catch (Exception v0_1) {
                Log.e("monitorLog", "packStatEntry json failed" + v0_1.toString());
                v0 = v1;
            }
        } else {
            v0 = v1;
        }

        return v0;
    }

    protected final void a() {
        JSONObject v0_3;
        Object v0_2;
        Iterator v4;
        HashMap v1;
        try {
            long v2 = System.currentTimeMillis() / 1000;
            v1 = this.a;
            synchronized (v1) {
                if (!this.a.isEmpty()) {
                    v4 = this.a.entrySet().iterator();
                    while (v4.hasNext()) {
                        v0_2 = ((Map.Entry) v4.next()).getValue();
                        if (v2 - ((a) v0_2).d <= (((long) this.h))) {
                            continue;
                        }

                        v4.remove();
                        v0_3 = e.a("count", ((a) v0_2), null);
                        if (v0_3 == null) {
                            continue;
                        }

                        this.a("count", v0_3.toString());
                    }
                }
            }
            v1 = this.b;
            synchronized (v1) {
                if (!this.b.isEmpty()) {
                    v4 = this.b.entrySet().iterator();
                    while (v4.hasNext()) {
                        v0_2 = ((Map.Entry) v4.next()).getValue();
                        if (v2 - ((b) v0_2).e <= (((long) this.h))) {
                            continue;
                        }

                        v4.remove();
                        v0_3 = e.a("timer", null, ((b) v0_2));
                        if (v0_3 == null) {
                            continue;
                        }

                        this.a("timer", v0_3.toString());
                    }
                }
            }
        } catch (Throwable v0_1) {
            Log.e("monitorLog", "handleLogToQueue function failed :" + v0_1.toString());
        }
    }

    protected final void a(String arg4, String arg5) {
        Object v2;
        com.bytedance.frameworks.core.a.b v0_1;
        if (!TextUtils.isEmpty(arg5) && !TextUtils.isEmpty(arg4)) {
            LinkedList v1 = this.f;
            synchronized (v1) {
                if (this.f.size() >= 2000) {
                    this.f.poll();
                }

                this.f.add(new com.bytedance.frameworks.core.a.a(arg4, arg5));
                v0_1 = this.d;
                v2 = v0_1.a;
                synchronized (v2) {
                    v0_1.a.notify();
                }
                return;
            }
        }
    }

    protected final void a(String arg5, String arg6, float arg7) {
        if (!TextUtils.isEmpty(arg5) && !TextUtils.isEmpty(arg6)) {
            JSONObject v0 = new JSONObject();
            try {
                v0.put("type", arg5);
                v0.put("key", arg6);
                v0.put("value", ((double) arg7));
                this.a("count", v0.toString());
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    protected final void b() {
        if (c.a() > 0) {
            this.h = com.bytedance.frameworks.core.a.c.a();
        }

        this.i = com.bytedance.frameworks.core.a.c.b();
        this.j = com.bytedance.frameworks.core.a.c.d();
        if (this.d != null) {
            com.bytedance.frameworks.core.a.b v0 = this.d;
            int v1 = this.h;
            if (v1 > 0) {
                v0.b = v1;
            }

            v0 = this.d;
            JSONObject v1_1 = this.j;
            if (v1_1 != null && v1_1.length() > 0) {
                v0.c = v1_1;
            }

            v0 = this.d;
            v1 = this.i;
            if (v1 <= 0) {
                return;
            }

            v0.e = v1;
        }
    }

    protected final void b(String arg5, String arg6, float arg7) {
        if (!TextUtils.isEmpty(((CharSequence) arg5)) && !TextUtils.isEmpty(((CharSequence) arg6))) {
            JSONObject v0 = new JSONObject();
            try {
                v0.put("type", arg5);
                v0.put("key", arg6);
                v0.put("value", ((double) arg7));
                this.a("timer", v0.toString());
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    protected final void c(String arg5, String arg6, float arg7) {
        if (!TextUtils.isEmpty(((CharSequence) arg5)) && !TextUtils.isEmpty(((CharSequence) arg6))) {
            JSONObject v0 = new JSONObject();
            try {
                v0.put("type", arg5);
                v0.put("key", arg6);
                v0.put("value", ((double) arg7));
                this.a("store", v0.toString());
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    public static final class a {
        public float c;
        String a;
        String b;
        long d;

        public a(String arg2, String arg3, long arg4) {

            this.a = arg2;
            this.b = arg3;
            this.c = 0f;
            this.d = arg4;
        }
    }

    public static final class b {
        public int c;
        public float d;
        String a;
        String b;
        long e;

        public b(String arg2, String arg3, long arg4) {
            this.a = arg2;
            this.b = arg3;
            this.c = 0;
            this.d = 0f;
            this.e = arg4;
        }
    }
}

